package com.ruoyi.module.excellentCase.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.module.excellentCase.domain.ExcellentCase;
import com.ruoyi.module.excellentCase.mapper.ExcellentCaseMapper;
import com.ruoyi.module.excellentCase.service.IExcellentCaseService;
import com.ruoyi.module.excellentCase.vo.ExcellentCaseVO;
import com.ruoyi.module.excellentCaseDetail.domain.ExcellentCaseDetail;
import com.ruoyi.module.excellentCaseDetail.mapper.ExcellentCaseDetailMapper;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 优秀案例Service业务层处理
 * 
 * @author yqq
 * @date 2024-04-18
 */
@Service
public class ExcellentCaseServiceImpl implements IExcellentCaseService 
{
    @Resource
    private ExcellentCaseMapper excellentCaseMapper;

    @Resource
    private ExcellentCaseDetailMapper excellentCaseDetailMapper;

    @Resource
    private ISysUserService userService;

    /**
     * 查询优秀案例
     * 
     * @param caseId 优秀案例主键
     * @return 优秀案例
     */
    @Override
    public ExcellentCase selectExcellentCaseByCaseId(Long caseId)
    {
        return excellentCaseMapper.selectExcellentCaseByCaseId(caseId);
    }

    /**
     * 查询优秀案例列表
     * 
     * @param excellentCase 优秀案例
     * @return 优秀案例
     */
    @Override
    public List<ExcellentCase> selectExcellentCaseList(ExcellentCase excellentCase)
    {
        return excellentCaseMapper.selectExcellentCaseList(excellentCase);
    }

    /**
     * 新增优秀案例
     * 
     * @param excellentCase 优秀案例
     * @return 结果
     */
    @Override
    public int insertExcellentCase(ExcellentCase excellentCase)
    {
        excellentCase.setCreateTime(DateUtils.getNowDate());
        return excellentCaseMapper.insertExcellentCase(excellentCase);
    }

    /**
     * 修改优秀案例
     * 
     * @param excellentCase 优秀案例
     * @return 结果
     */
    @Override
    public int updateExcellentCase(ExcellentCase excellentCase)
    {
        excellentCase.setUpdateTime(DateUtils.getNowDate());
        return excellentCaseMapper.updateExcellentCase(excellentCase);
    }

    /**
     * 批量删除优秀案例
     * 
     * @param caseIds 需要删除的优秀案例主键
     * @return 结果
     */
    @Override
    public int deleteExcellentCaseByCaseIds(Long[] caseIds)
    {
        return excellentCaseMapper.deleteExcellentCaseByCaseIds(caseIds);
    }

    /**
     * 删除优秀案例信息
     * 
     * @param caseId 优秀案例主键
     * @return 结果
     */
    @Override
    public int deleteExcellentCaseByCaseId(Long caseId)
    {
        return excellentCaseMapper.deleteExcellentCaseByCaseId(caseId);
    }

    /**
     * 新增案列及案列详情
     * @param excellentCaseVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertExcellentCaseAndDetail(ExcellentCaseVO excellentCaseVO) {
        ExcellentCase excellentCase = new ExcellentCase();
        BeanUtils.copyProperties(excellentCaseVO,excellentCase);
        excellentCase.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        excellentCase.setCreateTime(DateUtils.getNowDate());
        //获取作者信息
        String phone = SecurityUtils.getLoginUser().getUser().getPhonenumber();
        Map<String, String> authorInfo = userService.getAuthorInfo(phone);
        if (CollUtil.isNotEmpty(authorInfo)){
            excellentCase.setAuthorId(Long.valueOf(authorInfo.get("authorId")));
            excellentCase.setAuthorName(StringUtils.isNotBlank(authorInfo.get("memberNickname"))?authorInfo.get("memberNickname"):authorInfo.get("memberName"));
        }else {
            throw new BaseException(phone+"手机号注册会员");
        }
        excellentCaseMapper.insertExcellentCase(excellentCase);
        List<ExcellentCaseDetail> caseDetailList = excellentCaseVO.getCaseDetailList();
        if (CollUtil.isNotEmpty(caseDetailList)){
            for (ExcellentCaseDetail excellentCaseDetail : caseDetailList) {
                excellentCaseDetail.setdCaseId(excellentCase.getCaseId());
            }
            excellentCaseDetailMapper.insertAllExcellentCaseDetail(caseDetailList);
        }
        return AjaxResult.success();
    }

    /**
     * 修改优秀案例
     * @param excellentCaseVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateExcellentCaseVO(ExcellentCaseVO excellentCaseVO) {
        ExcellentCase excellentCase = new ExcellentCase();
        BeanUtils.copyProperties(excellentCaseVO,excellentCase);
        excellentCaseMapper.updateExcellentCase(excellentCase);
        //删除子集
        excellentCaseDetailMapper.deleteExcellentCaseDetailByDCaseid(excellentCase.getCaseId());

        List<ExcellentCaseDetail> caseDetailList = excellentCaseVO.getCaseDetailList();
        if (CollUtil.isNotEmpty(caseDetailList)){
            for (ExcellentCaseDetail excellentCaseDetail : caseDetailList) {
                excellentCaseDetail.setdCaseId(excellentCase.getCaseId());
            }
            excellentCaseDetailMapper.insertAllExcellentCaseDetail(caseDetailList);
        }
        return AjaxResult.success();
    }

    /**
     * 删除优秀案例
     * @param caseId 案例id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteAllExcellentCaseByCaseId(Long caseId) {
        excellentCaseMapper.deleteExcellentCaseByCaseId(caseId);
        excellentCaseDetailMapper.deleteExcellentCaseDetailByDCaseid(caseId);
        return AjaxResult.success();
    }
}
